package com.pangda.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pangda.common.domain.ResponseResult;
import com.pangda.pojo.Business;
import com.pangda.pojo.Clue;
import com.pangda.pojo.Contract;
import com.pangda.pojo.vo.IndexVo;
import com.pangda.service.IBusinessService;
import com.pangda.service.IClueService;
import com.pangda.service.IContractService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/index")
@Api(tags = "首页")
public class IndexController {
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IClueService clueService;
    @Autowired
    private IContractService contractService;

    @GetMapping
    public ResponseResult index(String beginCreateTime, String endCreateTime) {
        IndexVo indexVo = new IndexVo();
        if (StrUtil.isBlank(beginCreateTime) && StrUtil.isBlank(endCreateTime)) {
            return ResponseResult.error("请选择时间");
        }
        LambdaQueryWrapper<Business> businessWrapper = new LambdaQueryWrapper<>();
        businessWrapper.between(Business::getCreateTime, beginCreateTime, endCreateTime + " 23:59:59");

        int todayBusinessNum = businessService.count(businessWrapper);
        int tofollowedBusinessNum = businessService.count(businessWrapper);
        int toallocatedBusinessNum = businessService.count(businessWrapper);
        int businessNum = businessService.count(businessWrapper);

        indexVo.setBusinessNum(businessNum);
        indexVo.setToallocatedBusinessNum(toallocatedBusinessNum);
        indexVo.setTodayBusinessNum(todayBusinessNum);
        indexVo.setTofollowedBusinessNum(tofollowedBusinessNum);

        LambdaQueryWrapper<Clue> clueWrapper = new LambdaQueryWrapper<>();
        clueWrapper.between(Clue::getCreateTime, beginCreateTime, endCreateTime + " 23:59:59");
        int cluesNum = clueService.count(clueWrapper);
        indexVo.setCluesNum(cluesNum);

        LambdaQueryWrapper<Contract> contractWrapper = new LambdaQueryWrapper<>();
        contractWrapper.between(Contract::getCreateTime, beginCreateTime, endCreateTime + " 23:59:59");

        List<BigDecimal> numbers = new ArrayList<>();

        LambdaQueryWrapper<Contract> select = contractWrapper.select(Contract::getOrderAmount).isNotNull(Contract::getOrderAmount);
        contractService.list(select).forEach(contract -> {
            numbers.add(contract.getOrderAmount());
        });

        indexVo.setSalesAmount(numbers.stream().reduce(BigDecimal.ZERO, BigDecimal::add));

        int todayContractNum = contractService.count(contractWrapper);
        int todayCluesNum = contractService.count(contractWrapper);
        int tofollowedCluesNum = contractService.count(contractWrapper);
        int contractNum = contractService.count(contractWrapper);
        int toallocatedCluesNum = contractService.count(contractWrapper);
        indexVo.setContractNum(contractNum);
        indexVo.setToallocatedCluesNum(toallocatedCluesNum);
        indexVo.setTofollowedCluesNum(tofollowedCluesNum);
        indexVo.setTodayCluesNum(todayCluesNum);
        indexVo.setTodayContractNum(todayContractNum);

        List<Contract> list = contractService.list(new LambdaQueryWrapper<Contract>().isNotNull(Contract::getOrderAmount).between(Contract::getCreateTime, LocalDate.now().toString(), LocalDate.now() + " 23:59:59"));
        double sum = 0;
        for (Contract contract : list) {
            sum += contract.getOrderAmount().doubleValue();
        }

        indexVo.setTodaySalesAmount(BigDecimal.valueOf(sum));


        return ResponseResult.success(indexVo);
    }

    /**
     * 首页-商机转化龙虎榜
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/businessChangeStatistics")
    public ResponseResult businessChangeStatistics(String beginCreateTime, String endCreateTime) {
        return ResponseResult.success(businessService.getRank(beginCreateTime, endCreateTime));
    }

    /**
     * 首页-线索转换龙虎榜
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/salesStatistic")
    public ResponseResult salesStatistic(String beginCreateTime, String endCreateTime) {
        return ResponseResult.success(clueService.getTime(beginCreateTime, endCreateTime));
    }
}
